<template>
  <div class="student-experiments">
    <el-card>
      <div style="display: flex; justify-content: space-between; align-items: center;">
        <span style="font-size: 20px; font-weight: bold;">实验列表</span>
        <el-button type="primary" @click="refreshExperiments">刷新列表</el-button>
      </div>
      <el-table
        :data="experiments"
        v-loading="loading"
        style="width: 100%; margin-top: 20px;"
        border
      >
        <el-table-column prop="name" label="实验名称" />
        <el-table-column prop="category" label="分类" width="120">
          <template #default="{ row }">
            {{ categories.find(item => item.value === row.category)?.label || row.category }}
          </template>
        </el-table-column>
        <el-table-column prop="description" label="描述" />
        <el-table-column prop="difficulty" label="难度" width="130">
          <template #default="{ row }">
            <el-rate 
              :model-value="Number(row.difficulty)" 
              disabled 
              show-text 
              :texts="['简单', '初级', '中等', '困难', '挑战']" 
            />
          </template>
        </el-table-column>
        <el-table-column label="时间范围" width="200">
          <template #default="{ row }">
            <div v-if="row.start_time || row.end_time">
              <div>开始: {{ formatDate(row.start_time) }}</div>
              <div>结束: {{ formatDate(row.end_time) }}</div>
            </div>
            <span v-else>未设置</span>
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态" width="100">
          <template #default="{ row }">
            <el-tag :type="getStatusTagType(row.status)">
              {{ row.status || '未开始' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="120">
          <template #default="{ row }">
            <el-button type="primary" size="small" @click="goToExperiment(row.id)">进入</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>
import axios from 'axios';
import { ElMessage } from 'element-plus';

export default {
  name: 'StudentExperimentListView',
  data() {
    return {
      experiments: [],
      loading: false,
      categories: [
        { value: 'physics', label: 'Python实验' },
        { value: 'chemistry', label: 'OS实验' },
        { value: 'biology', label: 'SQL实验' },
        { value: 'computer', label: '计算机实验' },
      ]
    };
  },
  mounted() {
    this.fetchExperiments();
  },
  methods: {
    async fetchExperiments() {
        this.loading = true;
  try {
    const res = await axios.get('/api/experiments/');
    this.experiments = Array.isArray(res.data) ? res.data : res.data?.results || [];
  } catch (error) {
    ElMessage.error('获取实验列表失败: ' + (error.response?.data?.message || error.message));
  } finally {
    this.loading = false;
  }
    },
    refreshExperiments() {
      this.fetchExperiments();
    },
    goToExperiment(id) {
      this.$router.push(`/experiments/${id}`);
    },
    getStatusTagType(status) {
      switch (status) {
        case '已完成':
          return 'success';
        case '进行中':
          return 'primary';
        case '已过期':
          return 'danger';
        default:
          return 'info';
      }
    },
    formatDate(dateString) {
      if (!dateString) return '未设置';
      const date = new Date(dateString);
      return date.toLocaleDateString('zh-CN', {
        year: 'numeric',
        month: '2-digit',
        day: '2-digit'
      }).replace(/\//g, '-');
    }
  }
};
</script>

<style scoped>
.student-experiments {
  max-width: 900px;
  margin: 32px auto;
}
.el-card {
  margin-bottom: 20px;
}
</style>